package java6699.selection;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;
/*
* @Time:2022-8-30
* @Author:212106699周梓希
 * */
public class SelectionWin6699 extends Application {
    private Label label2;
    private Object[] list;
    //重写start方法
    @Override
    public void start(Stage primaryStage) {
        //建立树状结构
        VBox root = new VBox(30);
        //设置控件
        Label label1 = new Label("212106699:周梓希");
        Button button = new Button("随机选人");
        label2 = new Label("恭喜学号和姓名被选中！");
        //添加多个控件
        root.getChildren().addAll(label1,button,label2);
        root.setAlignment(Pos.CENTER);
        //创建Scene
        Scene scene = new Scene(root,400,300);
        primaryStage.setScene(scene);
        primaryStage.setTitle("随机选人");
        primaryStage.show();
        //从StudentList.txt中读取学生名单--->数组
        try{
            list = Files.lines(Paths.get("data/StudentList.txt")).toArray();
        }catch (Exception e){
            e.printStackTrace();
        }
        //创建监听对象
        EventHandler<ActionEvent> handler = new  MyHandler();
        //注册监听
        button.setOnAction(handler);
    }
    //实现监听接口，在事件处理方法中完成任务
    class MyHandler implements EventHandler<ActionEvent> {
        @Override
        public void handle(ActionEvent event) {
            //随机数生成0-数组长度
            Random random = new Random();
            int r = random.nextInt(list.length);
            //从数组中读取元素
            Object resutlt = list[r];
            label2.setText("恭喜"+(String)resutlt+"被选中！");
        }
    }
    //调用Application的静态方法launch启动JavaFX程序
    public static void main(String[] args) {
        launch(args);
    }
}
